<template>
    <view class="columnbox wdh-100 content">
        <view class="rowbox  spb one-info">
            <view>企业名称</view>
            <input v-model="form.companyName" placeholder="请输入" />
        </view>
        <view class="rowbox  spb one-info">
            <view>证件类型</view>
            营业执照
        </view>
        <view class="rowbox  spb one-info">
            <view>组织机构代码</view>
            <input v-model="form.creditCode" placeholder="请输入" />
        </view>
        <view class="rowbox  spb one-info">
            <view>联系方式</view>
            <input v-model="form.phone" placeholder="请输入" />
        </view>
        <view class="rowbox  spb one-info">
            <view>营业执照</view>
            <view class="rowbox add-img" @click="addImg" v-if="imgurl==''">
                <u-icon name="plus"></u-icon>
            </view>
            <view v-else class="rowbox add-img" @click="addImg">
                <image :src="imgurl" mode="aspectFill"></image>
            </view>
        </view>
        <view class="small-btn one-btn" @click="submitInfo">提交认证</view>
    </view>
</template>

<script>
    export default {
        data() {
            return {
                form: {
                    companyName: '',
                    creditCode: '',
                    phone: '',
                    url: '',
                    companyType: ''
                },
                imgurl: '',
                submitflag: false,
                updateflag: false,
                updateinfo: ''
            };
        },
        onLoad(options) {
            if (options.id) {
                this.id = options.id
                this.updateflag = true
                this.getDetail()
            }
        },
        methods: {
            getDetail() {
                this.$req.get(`/xcx/busIcense/${this.id}`).then(res => {
                    this.updateinfo = res.data.data
                    console.log(res.data)
                    this.form = {
                        companyName: res.data.data.companyName,
                        creditCode: res.data.data.creditCode,
                        phone: res.data.data.phone,
                        url: res.data.data.url,
                        companyType: res.data.data.companyType,
                    }
                    this.imgurl = res.data.data.url
                })
            },
            submitInfo() {
                if (this.form.companyName == '') {
                    uni.showToast({
                        title: '请输入企业名称',
                        icon: 'none'
                    })
                    return false
                }
                if (this.form.creditCode == '') {
                    uni.showToast({
                        title: '请输入组织机构代码',
                        icon: 'none'
                    })
                    return false
                }
                if (this.form.phone == '') {
                    uni.showToast({
                        title: '请输入联系方式',
                        icon: 'none'
                    })
                    return false
                }
                if (this.imgurl == '') {
                    uni.showToast({
                        title: '请上传营业执照',
                        icon: 'none'
                    })
                    return false
                }
                if (!this.submitflag) {
                    uni.showLoading({
                        title: '正在提交'
                    })
                    this.submitflag = true
                    let data = Object.assign({}, this.form, {
                        uid: uni.getStorageSync('uid')
                    })
                    if (this.updateflag) {
                        data.userId = this.updateinfo.userId
                    }
                    this.$req.post('/xcx/busIcense/verified', data).then(res => {
                        uni.hideLoading()
                        this.submitflag = false
                        uni.showToast({
                            title: res.data.msg,
                            icon: 'none'
                        })
                        if (res.data.code == 200) {
                            let pages = getCurrentPages(); //获取所有页面栈实例列表
                            let nowPage = pages[pages.length -
                                1]; //当前页页面实例
                            let prevPage = pages[pages.length -
                                2]; //上一页页面实例
                            prevPage.$vm.refreshflag = true
                            uni.navigateBack()
                        }
                    })
                }
            },
            addImg() {
                uni.chooseImage({
                    count: 1,
                    success: (res) => {
                        this.imgurl = res.tempFilePaths[0]
                        uni.showLoading({
                            title: '加载中'
                        })
                        this.$req.uploadImg(this.imgurl).then(res => {
                            this.form.url = res.data.url
                            this.getImgInfo()
                        })
                    }
                })
            },
            getImgInfo() {
                this.$req.post('/xcx/busIcense/ocr', {
                    url: this.form.url
                }).then(res => {
                    uni.hideLoading()
                    if (res.data.code == 200) {
                        let data = {
                            companyType: res.data.data.companyType,
                            companyName: res.data.data.companyName,
                            creditCode: res.data.data.creditCode,
                            phone: res.data.data.phone,
                            url: this.form.url
                        }
                        this.form = data
                    } else {
                        uni.showModal({
                            title: res.data.msg
                        })
                    }
                })
            }
        }
    }
</script>

<style lang="scss" scoped>
    .content {
        font-family: PingFang SC;
    }

    .one-info {
        width: 710rpx;
        padding: 32rpx 20rpx;
        border-bottom: #eef0f7 solid 1px;
        font-size: 28rpx;

        input {
            text-align: right;
            font-size: 28rpx;
            width: 500rpx;
        }
    }

    .one-type {
        font-size: 24rpx;
        border: #eef0f7 solid 1px;
        height: 70rpx;
        padding: 0 20rpx;
        border-radius: 10rpx;
        margin-left: 20rpx;
    }

    .sel-type {
        background: #17698F;
        color: #fff;
    }

    .one-btn {
        background: #17698F;
        color: #fff;
        width: 710rpx;
        height: 90rpx;
        border-radius: 20rpx;
        margin-top: 40rpx;
        font-size: 28rpx;
    }

    .notice-info {
        font-size: 28rpx;
        margin-top: 20rpx;
    }

    .document-box {
        border-radius: 20rpx;
        background: #efefef;
        width: 710rpx;
        padding: 32rpx 32rpx;
        margin-top: 40rpx;
        font-size: 28rpx;
        margin-bottom: 20rpx
    }

    .d-title {
        font-size: 32rpx;
        width: 100%;
        margin-bottom: 20rpx;
    }

    .xichetype-desc {
        font-size: 24rpx;
        color: #6C6C6C;
        margin-top: 20rpx;
    }

    .add-img {
        width: 120rpx;
        height: 120rpx;
        border: #c9ccd4 dotted 1px;
        border-radius: 10rpx;
        position: relative;
        margin-bottom: 10rpx;

        image {
            width: 120rpx;
            height: 120rpx;
            border-radius: 10rpx;
        }

        .delet-box {
            position: absolute;
            right: 0;
            top: 0;
            background: rgba(#fff, .7);
            border-radius: 100rpx;
        }
    }
</style>